package icu.lookyousmileface.nio.basic

import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.SocketChannel

/**
 * @title: NioClient
 * @projectName NettyPro
 * @author shenj
 * @date 2020/12/1 18:57
 */
fun main(args: Array<String>):Unit {

    val data = "我深深地熟悉你脚步的韵律,它在我心中敲击."
    val socketChannel = SocketChannel.open();

    socketChannel.configureBlocking(false);

    val inetSocketAddress = InetSocketAddress("127.0.0.1",7798)

    if(!socketChannel.connect(inetSocketAddress)){
        while (!socketChannel.finishConnect()){
            System.out.println("连接服务器，需要时间....出去溜达会吧~")
        }
    }

    val dataBuffer = ByteBuffer.wrap(data.toByteArray(Charsets.UTF_8))
    socketChannel.write(dataBuffer)
    System.`in`.read()
}